import os,sys,random,math,pygame,time,screen
from pygame.locals import *

import Client,Server

class Main:
    def __init__(self):
        self.running = 1
        self.MenuOption = 'Main'
        self.Type = 'None'
        self.pressed = 0

    def _UpdateDisplay(self):
        if self.MenuOption == 'Main':
            screen.screen.blit(screen.Main_1024_768,(0,0))
        if self.MenuOption == 'Game':
            screen.screen.blit(screen.Game_1024_768,(0,0))
        if self.MenuOption == 'Mult':
            screen.screen.blit(screen.Mult_1024_768,(0,0))
        if self.MenuOption == 'Join':
            screen.screen.blit(screen.Join_1024_768,(0,0))

    def _RunDisplay(self):
        screen.screen.fill((0,0,0))
        self._UpdateDisplay()
        pygame.display.flip()

    def _RunGame(self):
        if self.MenuOption == 'Main':
            if pygame.mouse.get_pressed()[0] and self.pressed == 1:
                pos = pygame.mouse.get_pos()
                if pos[0] > 125 and pos[1] > 440:
                    if pos[0] < 260 and pos[1] < 490:
                        self.running = 0
                if pos[0] > 125 and pos[1] > 260:
                    if pos[0] < 510 and pos [1] < 330:
                        self.MenuOption = 'Mult'
                        self.pressed = 0
        if self.MenuOption == 'Mult':
            if pygame.mouse.get_pressed()[0] and self.pressed == 1:
                pos = pygame.mouse.get_pos()
                if pos[0] > 125 and pos[1] > 170:
                    if pos[0] < 545 and pos[1] < 225:
                        self.MenuOption = 'Game'
                        self.Type = 'Create'
                        self._MakeGame()
                        self.pressed = 0
                if pos[0] > 125 and pos[1] > 260:
                    if pos[0] < 470 and pos[0] < 315:
                        self.MenuOption = 'Join'
                        self.pressed = 0
                if pos[0] > 125 and pos[1] > 350:
                    if pos[0] < 290 and pos[1] < 405:
                        self.MenuOption = 'Main'
                        self.pressed = 0
        if self.MenuOption == 'Join':
            if pygame.mouse.get_pressed()[0] and self.pressed == 1:
                pos = pygame.mouse.get_pos()
                if pos[0] > 125 and pos[1] > 440:
                    if pos[0] < 290 and pos[1] < 495:
                        self.MenuOption = 'Mult'
                        self.pressed = 0
                if pos[0] > 125 and pos[1] > 350:
                    if pos[0] < 270 and pos[1] < 400:
                        self.MenuOption = 'Game'
                        self._JoinGame()
                        self.pressed = 0
        if self.pressed == 0:
            if not pygame.mouse.get_pressed()[0]:
                self.pressed = 1
                    

    def _Exit(self):
        pygame.display.quit()
        self.Server.Close()

    def _MakeGame(self):
        port = input("port:")
        self.Server = Server.TCPServerModule("", port)

    def _JoinGame(self):
        self.Client = Client.TCPClientModule()
        name = 'localhost'
        port = input('port:')
        for attempt in range(5):
            m = self.Client._AttemptConnect(name,port)
            print m
            if m == True:
                break

    def _NetworkManager(self):
        pass

    def Game(self):
        while self.running:
            pygame.time.Clock().tick(60)
            self._RunDisplay()
            self._RunGame()

            for event in pygame.event.get():
                if event.type == QUIT:
                    self.running = 0
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        self.running = 0

        self._Exit()
    
if __name__ == "__main__":
    main = Main()
    main.Game()
